﻿using Leeder.Logger;
using LeederGlobal;
using SPSAndong.Business;
using SPSAndong.Comm;
using SPSAndong.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace SPSAndong.Forms
{
    public partial class FrmBoradcastSet : UIPage
    {

        public LogOperate LogObj => Log.GetLogger(this.GetType());
        AutoSizeFormClass asc = new AutoSizeFormClass();
        bool isLoad = true;      //判断窗体是否首次加载

        Dictionary<int, UILabel> DicLabelSNs = new Dictionary<int, UILabel>();
        Dictionary<int, UITextBox> DicTextAreaNames = new Dictionary<int, UITextBox>();
        Dictionary<int, UIComboBox> DicCbAreas = new Dictionary<int, UIComboBox>();
        Dictionary<int, UITextBox> DicTextIPs = new Dictionary<int, UITextBox>();
        Dictionary<int, UISwitch> DicSwitchBroadcastNFs = new Dictionary<int, UISwitch>();
        Dictionary<int, UILabel> DicLabelStates = new Dictionary<int, UILabel>();
        Dictionary<int, UISymbolButton> DicSymbolVoices = new Dictionary<int, UISymbolButton>();
        Dictionary<int, UITrackBar> DicTackBarVolumes = new Dictionary<int, UITrackBar>();
        Dictionary<int, UISymbolButton> DicBtnPlays = new Dictionary<int, UISymbolButton>();
        Dictionary<int, UISymbolButton> DicBtnStops = new Dictionary<int, UISymbolButton>();
        Dictionary<int, UISymbolButton> DicBtnPreps = new Dictionary<int, UISymbolButton>();
        Dictionary<int, UISymbolButton> DicBtnAdds = new Dictionary<int, UISymbolButton>();

        /// <summary>
        /// 广播设置集合
        /// </summary>
        List<BroadcastSetModel> broadcastSetList;

        MusicSetBLL musicSetBLL = new MusicSetBLL();
        BroadcastSetBLL broadcastSetBLL = new BroadcastSetBLL();

        public FrmBoradcastSet()
        {
            InitializeComponent();
        }

        private void FrmBoradcastSet_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;
            asc.controllInitializeSize(this);
            Init_Control();

            LoadBoradcastSet();
        }

        private void FrmBoradcastSet_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
        }

        private void Init_Control()
        {
            DicLabelSNs.Add(1, labelSN1);
            DicLabelSNs.Add(2, labelSN2);
            DicLabelSNs.Add(3, labelSN3);
            DicLabelSNs.Add(4, labelSN4);
            DicLabelSNs.Add(5, labelSN5);
            DicLabelSNs.Add(6, labelSN6);
            DicLabelSNs.Add(7, labelSN7);
            DicLabelSNs.Add(8, labelSN8);
            DicLabelSNs.Add(9, labelSN9);
            DicLabelSNs.Add(10, labelSN10);
            DicLabelSNs.Add(11, labelSN11);
            DicLabelSNs.Add(12, labelSN12);
            DicLabelSNs.Add(13, labelSN13);
            DicLabelSNs.Add(14, labelSN14);
            DicLabelSNs.Add(15, labelSN15);

            DicTextAreaNames.Add(1, txtAreaName1);
            DicTextAreaNames.Add(2, txtAreaName2);
            DicTextAreaNames.Add(3, txtAreaName3);
            DicTextAreaNames.Add(4, txtAreaName4);
            DicTextAreaNames.Add(5, txtAreaName5);
            DicTextAreaNames.Add(6, txtAreaName6);
            DicTextAreaNames.Add(7, txtAreaName7);
            DicTextAreaNames.Add(8, txtAreaName8);
            DicTextAreaNames.Add(9, txtAreaName9);
            DicTextAreaNames.Add(10, txtAreaName10);
            DicTextAreaNames.Add(11, txtAreaName11);
            DicTextAreaNames.Add(12, txtAreaName12);
            DicTextAreaNames.Add(13, txtAreaName13);
            DicTextAreaNames.Add(14, txtAreaName14);
            DicTextAreaNames.Add(15, txtAreaName15);

            DicCbAreas.Add(1,  cbArea1);
            DicCbAreas.Add(2,  cbArea2);
            DicCbAreas.Add(3,  cbArea3);
            DicCbAreas.Add(4,  cbArea4);
            DicCbAreas.Add(5,  cbArea5);
            DicCbAreas.Add(6,  cbArea6);
            DicCbAreas.Add(7,  cbArea7);
            DicCbAreas.Add(8,  cbArea8);
            DicCbAreas.Add(9,  cbArea9);
            DicCbAreas.Add(10, cbArea10);
            DicCbAreas.Add(11, cbArea11);
            DicCbAreas.Add(12, cbArea12);
            DicCbAreas.Add(13, cbArea13);
            DicCbAreas.Add(14, cbArea14);
            DicCbAreas.Add(15, cbArea15);

            DicTextIPs.Add(1,  txtIP1);
            DicTextIPs.Add(2,  txtIP2);
            DicTextIPs.Add(3,  txtIP3);
            DicTextIPs.Add(4,  txtIP4);
            DicTextIPs.Add(5,  txtIP5);
            DicTextIPs.Add(6,  txtIP6);
            DicTextIPs.Add(7,  txtIP7);
            DicTextIPs.Add(8,  txtIP8);
            DicTextIPs.Add(9,  txtIP9);
            DicTextIPs.Add(10, txtIP10);
            DicTextIPs.Add(11, txtIP11);
            DicTextIPs.Add(12, txtIP12);
            DicTextIPs.Add(13, txtIP13);
            DicTextIPs.Add(14, txtIP14);
            DicTextIPs.Add(15, txtIP15);

            DicSwitchBroadcastNFs.Add(1,  swBroadcastNF1);
            DicSwitchBroadcastNFs.Add(2,  swBroadcastNF2);
            DicSwitchBroadcastNFs.Add(3,  swBroadcastNF3);
            DicSwitchBroadcastNFs.Add(4,  swBroadcastNF4);
            DicSwitchBroadcastNFs.Add(5,  swBroadcastNF5);
            DicSwitchBroadcastNFs.Add(6,  swBroadcastNF6);
            DicSwitchBroadcastNFs.Add(7,  swBroadcastNF7);
            DicSwitchBroadcastNFs.Add(8,  swBroadcastNF8);
            DicSwitchBroadcastNFs.Add(9,  swBroadcastNF9);
            DicSwitchBroadcastNFs.Add(10, swBroadcastNF10);
            DicSwitchBroadcastNFs.Add(11, swBroadcastNF11);
            DicSwitchBroadcastNFs.Add(12, swBroadcastNF12);
            DicSwitchBroadcastNFs.Add(13, swBroadcastNF13);
            DicSwitchBroadcastNFs.Add(14, swBroadcastNF14);
            DicSwitchBroadcastNFs.Add(15, swBroadcastNF15);

            DicLabelStates.Add(1,  labelState1);
            DicLabelStates.Add(2,  labelState2);
            DicLabelStates.Add(3,  labelState3);
            DicLabelStates.Add(4,  labelState4);
            DicLabelStates.Add(5,  labelState5);
            DicLabelStates.Add(6,  labelState6);
            DicLabelStates.Add(7,  labelState7);
            DicLabelStates.Add(8,  labelState8);
            DicLabelStates.Add(9,  labelState9);
            DicLabelStates.Add(10, labelState10);
            DicLabelStates.Add(11, labelState11);
            DicLabelStates.Add(12, labelState12);
            DicLabelStates.Add(13, labelState13);
            DicLabelStates.Add(14, labelState14);
            DicLabelStates.Add(15, uiLabel39);

            DicSymbolVoices.Add(1,  symbolVoice1);
            DicSymbolVoices.Add(2,  symbolVoice2);
            DicSymbolVoices.Add(3,  symbolVoice3);
            DicSymbolVoices.Add(4,  symbolVoice4);
            DicSymbolVoices.Add(5,  symbolVoice5);
            DicSymbolVoices.Add(6,  symbolVoice6);
            DicSymbolVoices.Add(7,  symbolVoice7);
            DicSymbolVoices.Add(8,  symbolVoice8);
            DicSymbolVoices.Add(9,  symbolVoice9);
            DicSymbolVoices.Add(10, symbolVoice10);
            DicSymbolVoices.Add(11, symbolVoice11);
            DicSymbolVoices.Add(12, symbolVoice12);
            DicSymbolVoices.Add(13, symbolVoice13);
            DicSymbolVoices.Add(14, symbolVoice14);
            DicSymbolVoices.Add(15, symbolVoice15);
            DicSymbolVoices.Add(16, symbolVoice16);
            DicSymbolVoices.Add(17, symbolVoice17);
            DicSymbolVoices.Add(18, symbolVoice18);
            DicSymbolVoices.Add(19, symbolVoice19);
            DicSymbolVoices.Add(20, symbolVoice20);
            DicSymbolVoices.Add(21, symbolVoice21);
            DicSymbolVoices.Add(22, symbolVoice22);
            DicSymbolVoices.Add(23, symbolVoice23);
            DicSymbolVoices.Add(24, symbolVoice24);
            DicSymbolVoices.Add(25, symbolVoice25);
            DicSymbolVoices.Add(26, symbolVoice26);
            DicSymbolVoices.Add(27, symbolVoice27);
            DicSymbolVoices.Add(28, symbolVoice28);
            DicSymbolVoices.Add(29, symbolVoice29);
            DicSymbolVoices.Add(30, symbolVoice30);

            DicTackBarVolumes.Add(1,  trackBarVolume1);
            DicTackBarVolumes.Add(2,  trackBarVolume2);
            DicTackBarVolumes.Add(3,  trackBarVolume3);
            DicTackBarVolumes.Add(4,  trackBarVolume4);
            DicTackBarVolumes.Add(5,  trackBarVolume5);
            DicTackBarVolumes.Add(6,  trackBarVolume6);
            DicTackBarVolumes.Add(7,  trackBarVolume7);
            DicTackBarVolumes.Add(8,  trackBarVolume8);
            DicTackBarVolumes.Add(9,  trackBarVolume9);
            DicTackBarVolumes.Add(10, trackBarVolume10);
            DicTackBarVolumes.Add(11, trackBarVolume11);
            DicTackBarVolumes.Add(12, trackBarVolume12);
            DicTackBarVolumes.Add(13, trackBarVolume13);
            DicTackBarVolumes.Add(14, trackBarVolume14);
            DicTackBarVolumes.Add(15, trackBarVolume15);

            DicBtnPlays.Add(1,  btnPlay_1);
            DicBtnPlays.Add(2,  btnPlay_2);
            DicBtnPlays.Add(3,  btnPlay_3);
            DicBtnPlays.Add(4,  btnPlay_4);
            DicBtnPlays.Add(5,  btnPlay_5);
            DicBtnPlays.Add(6,  btnPlay_6);
            DicBtnPlays.Add(7,  btnPlay_7);
            DicBtnPlays.Add(8,  btnPlay_8);
            DicBtnPlays.Add(9,  btnPlay_9);
            DicBtnPlays.Add(10, btnPlay_10);
            DicBtnPlays.Add(11, btnPlay_11);
            DicBtnPlays.Add(12, btnPlay_12);
            DicBtnPlays.Add(13, btnPlay_13);
            DicBtnPlays.Add(14, btnPlay_14);
            DicBtnPlays.Add(15, btnPlay_15);

            DicBtnStops.Add(1,  btnStop_1);
            DicBtnStops.Add(2,  btnStop_2);
            DicBtnStops.Add(3,  btnStop_3);
            DicBtnStops.Add(4,  btnStop_4);
            DicBtnStops.Add(5,  btnStop_5);
            DicBtnStops.Add(6,  btnStop_6);
            DicBtnStops.Add(7,  btnStop_7);
            DicBtnStops.Add(8,  btnStop_8);
            DicBtnStops.Add(9,  btnStop_9);
            DicBtnStops.Add(10, btnStop_10);
            DicBtnStops.Add(11, btnStop_11);
            DicBtnStops.Add(12, btnStop_12);
            DicBtnStops.Add(13, btnStop_13);
            DicBtnStops.Add(14, btnStop_14);
            DicBtnStops.Add(15, btnStop_15);

            DicBtnPreps.Add(1,  btnPrep_1);
            DicBtnPreps.Add(2,  btnPrep_2);
            DicBtnPreps.Add(3,  btnPrep_3);
            DicBtnPreps.Add(4,  btnPrep_4);
            DicBtnPreps.Add(5,  btnPrep_5);
            DicBtnPreps.Add(6,  btnPrep_6);
            DicBtnPreps.Add(7,  btnPrep_7);
            DicBtnPreps.Add(8,  btnPrep_8);
            DicBtnPreps.Add(9,  btnPrep_9);
            DicBtnPreps.Add(10, btnPrep_10);
            DicBtnPreps.Add(11, btnPrep_11);
            DicBtnPreps.Add(12, btnPrep_12);
            DicBtnPreps.Add(13, btnPrep_13);
            DicBtnPreps.Add(14, btnPrep_14);
            DicBtnPreps.Add(15, btnPrep_15);

            DicBtnAdds.Add(1,  btnAdd_1);
            DicBtnAdds.Add(2,  btnAdd_2);
            DicBtnAdds.Add(3,  btnAdd_3);
            DicBtnAdds.Add(4,  btnAdd_4);
            DicBtnAdds.Add(5,  btnAdd_5);
            DicBtnAdds.Add(6,  btnAdd_6);
            DicBtnAdds.Add(7,  btnAdd_7);
            DicBtnAdds.Add(8,  btnAdd_8);
            DicBtnAdds.Add(9,  btnAdd_9);
            DicBtnAdds.Add(10, btnAdd_10);
            DicBtnAdds.Add(11, btnAdd_11);
            DicBtnAdds.Add(12, btnAdd_12);
            DicBtnAdds.Add(13, btnAdd_13);
            DicBtnAdds.Add(14, btnAdd_14);
            DicBtnAdds.Add(15, btnAdd_15);

            foreach (var area in DicTextAreaNames)
            {
                area.Value.RectColor = Color.White;
            }

            var list = musicSetBLL.GetModelAll();
            foreach (var area in DicCbAreas)
            {
                area.Value.RectColor = Color.WhiteSmoke;

                area.Value.DataSource = list;
                area.Value.DisplayMember = "AreaName";
                area.Value.ValueMember = "AreaType";
                area.Value.SelectedIndex = -1;
            }      

            foreach (var ip in DicTextIPs)
            {
                ip.Value.RectColor = Color.White;
            }

            foreach (var state in DicLabelStates)
            {
                state.Value.Text = "●未配置";
                state.Value.ForeColor = Color.LightGray;
            }

            foreach (var symbol in DicSymbolVoices)
            {
                symbol.Value.FillDisableColor = Color.White;
                symbol.Value.ForeDisableColor = Color.FromArgb(80, 160, 255);
                symbol.Value.RectDisableColor = Color.Gainsboro;
            }

            foreach (var Volume in DicTackBarVolumes)
            {
                Volume.Value.FillColor = Color.White;
                Volume.Value.Enabled = false;
            }

            foreach (var play in DicBtnPlays)
            {
                play.Value.Style =UIStyle.White;
                play.Value.ForeColor = Color.FromArgb(80, 160, 255);
                play.Value.Enabled = false;
            }

            foreach (var stop in DicBtnStops)
            {
                stop.Value.Style = UIStyle.White;
                stop.Value.ForeColor = Color.FromArgb(80, 160, 255);
                stop.Value.Enabled = false;
            }

            foreach (var prep in DicBtnPreps)
            {
                prep.Value.Style = UIStyle.White;
                prep.Value.ForeColor = Color.FromArgb(80, 160, 255);
                prep.Value.Enabled = false;
            }

            foreach (var add in DicBtnAdds)
            {
                add.Value.Style = UIStyle.White;
                add.Value.ForeColor = Color.FromArgb(80, 160, 255);
                add.Value.Enabled = false;
            }
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            UISymbolButton btn = (UISymbolButton)sender;
            var item = broadcastSetList.Where(x => x.ID == int.Parse(btn.Name.Split('_')[1])).FirstOrDefault();
            GolbalVariable.VoiceSever.ExecVoiceCommand(item.AreaType, true, item.IPAddress);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            UISymbolButton btn = (UISymbolButton)sender;
            var item = broadcastSetList.Where(x => x.ID == int.Parse(btn.Name.Split('_')[1])).FirstOrDefault();
            GolbalVariable.VoiceSever.ExecVoiceCommand(item.AreaType, false, item.IPAddress);
        }

        private void btnPrep_Click(object sender, EventArgs e)
        {
            UISymbolButton btn = (UISymbolButton)sender;
            btn.Enabled = false;
            var item = broadcastSetList.Where(x => x.ID == int.Parse(btn.Name.Split('_')[1])).FirstOrDefault();
            GolbalVariable.VoiceSever.ExecControlVoiceVolumeCommand(item.AreaType, false, item.IPAddress);
            var control = this.Controls.Find("trackBarVolume" + int.Parse(btn.Name.Split('_')[1]), true);
            if (control != null && control.Length > 0)
            {
                UITrackBar trackBar = control[0] as UITrackBar;
                trackBar.Value--;
            }
            Thread.Sleep(50);
            btn.Enabled = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            UISymbolButton btn = (UISymbolButton)sender;
            btn.Enabled = false;
            var item = broadcastSetList.Where(x => x.ID == int.Parse(btn.Name.Split('_')[1])).FirstOrDefault();
            GolbalVariable.VoiceSever.ExecControlVoiceVolumeCommand(item.AreaType, true, item.IPAddress);
            var control = this.Controls.Find("trackBarVolume" + int.Parse(btn.Name.Split('_')[1]), true);
            if (control != null && control.Length > 0)
            {
                UITrackBar trackBar = control[0] as UITrackBar;
                trackBar.Value++;
            }
            Thread.Sleep(20);
            btn.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            bool isNew = false;
            GetBoradcastSet(ref isNew);
            if (isNew)
            {
                if (broadcastSetBLL.AddList(broadcastSetList).IsSuccess)
                    UIMessageTip.ShowOk("保存成功");
                else
                    UIMessageBox.ShowError("保存失败");
            }
            else
            {
                if (broadcastSetBLL.UpdateList(broadcastSetList).IsSuccess)
                    UIMessageTip.ShowOk("保存成功");
                else
                    UIMessageBox.ShowError("保存失败");
            }
            //更新音乐参数缓存
            GolbalVariable.VoiceAreaMusicList = musicSetBLL.GetMusicBroadcastSet();
        }

        private void btnVolumeCalibration_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            ShowWaitForm("正在校正音量，预计需要几十秒，请耐心等候...");
            foreach (var item in broadcastSetList)
            {
                var linkState = GolbalVariable.VoiceSever.disSocClient.ContainsKey(item.IPAddress);
                if (linkState)
                {
                    var trackBar = DicTackBarVolumes[int.Parse(item.ID.ToString())];
                    if (trackBar != null)
                    {
                        for (int i = 0; i < 30; i++)
                        {
                            var broadcastSet = broadcastSetList.Where(x => x.ID == item.ID).FirstOrDefault();
                            GolbalVariable.VoiceSever.ExecControlVoiceVolumeCommand(broadcastSet.AreaType, false, broadcastSet.IPAddress);
                            Thread.Sleep(80);
                        }
                        for (int i = 0; i < trackBar.Value; i++)
                        {
                            var broadcastSet = broadcastSetList.Where(x => x.ID == item.ID).FirstOrDefault();
                            GolbalVariable.VoiceSever.ExecControlVoiceVolumeCommand(broadcastSet.AreaType, true, broadcastSet.IPAddress);
                            Thread.Sleep(80);
                        }
                    }
                }
                else
                {

                }
            }
            HideWaitForm();
        }

        /// <summary>
        /// 获取广播配置
        /// </summary>
        private void GetBoradcastSet(ref bool isNew)
        {
            broadcastSetList = broadcastSetBLL.GetModelAll();
            if (broadcastSetList != null && broadcastSetList.Count > 0)
            {
                isNew = false;
                foreach (var item in broadcastSetList)
                {
                    int index = int.Parse(item.ID.ToString());
                    item.ID = Convert.ToInt16(DicLabelSNs[index].Text);
                    item.IPAddress = DicTextIPs[index].Text;
                    item.BroadcastState = DicSwitchBroadcastNFs[index].Active;
                    item.BroadcastVolume = DicTackBarVolumes[index].Value;
                    item.AreaType = (byte)DicCbAreas[index].SelectedValue;
                }
            }
            else
            {
                isNew = true;
                broadcastSetList = new List<BroadcastSetModel>();
                for (int i = 1;i<= DicLabelSNs.Count;i++)
                {
                    BroadcastSetModel item = new BroadcastSetModel();
                    item.ID = Convert.ToInt16(DicLabelSNs[i].Text);
                    item.IPAddress = DicTextIPs[i].Text;
                    item.BroadcastState = DicSwitchBroadcastNFs[i].Active;
                    item.BroadcastVolume = DicTackBarVolumes[i].Value;
                    item.AreaType = (byte)DicCbAreas[i].SelectedValue;
                    item.AreaName = DicTextAreaNames[i].Text;
                    broadcastSetList.Add(item);
                }
            }
            //去空IP
            broadcastSetList = broadcastSetList.Where(x => !string.IsNullOrEmpty(x.IPAddress)).ToList();
        }

        /// <summary>
        /// 加载广播配置
        /// </summary>
        private void LoadBoradcastSet()
        {
            try
            {
                broadcastSetList = broadcastSetBLL.GetModelAll();
                LogObj.Info("广播配置数量：" + broadcastSetList.Count);
                if (broadcastSetList != null && broadcastSetList.Count > 0)
                {
                    for (int i = 0; i < broadcastSetList.Count; i++)
                    {
                        DicTextAreaNames[i+1].Text = broadcastSetList[i].AreaName;
                        DicLabelSNs[i + 1].Text = broadcastSetList[i].ID.ToString();
                        DicTextIPs[i + 1].Text = broadcastSetList[i].IPAddress;
                        DicSwitchBroadcastNFs[i + 1].Active = broadcastSetList[i].BroadcastState;
                        DicTackBarVolumes[i + 1].Value = broadcastSetList[i].BroadcastVolume;
                        DicCbAreas[i + 1].SelectedValue = broadcastSetList[i].AreaType;
                        SetButtonState(DicLabelStates[i + 1], DicBtnPlays[i + 1], DicBtnPreps[i + 1], DicBtnAdds[i + 1], DicBtnStops[i + 1], DicTackBarVolumes[i + 1], broadcastSetList[i].IPAddress);
                    }
                }
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.Message);
            }
        }

        /// <summary>
        /// 广播状态
        /// </summary>
        /// <param name="linkState"></param>
        public void SetButtonState(Label labelState, UISymbolButton btnPlay, UISymbolButton btnPrep, UISymbolButton btnAdd, UISymbolButton btnStop, UITrackBar trackBar, string ip)
        {
            var linkState = GolbalVariable.VoiceSever.disSocClient.ContainsKey(ip);
            if (linkState)
            {
                labelState.Text = "●在线";
                labelState.ForeColor = Color.Black;
                labelState.BackColor = Color.LimeGreen;
                btnPlay.Enabled = true;
                btnPrep.Enabled = true;
                btnAdd.Enabled = true;
                btnStop.Enabled = true;
                trackBar.Enabled = false;
            }
            else
            {
                labelState.Text = "●离线";
                labelState.ForeColor = Color.Red;
                labelState.BackColor = Color.FromArgb(244, 244, 244);
                btnPlay.Enabled = false;
                btnPrep.Enabled = false;
                btnAdd.Enabled = false;
                btnStop.Enabled = false;
                trackBar.Enabled = false;
            }
        }
    }
}
